package com.lagou.controller;

import com.lagou.domain.ResourceCategory;
import com.lagou.domain.ResponseResult;
import com.lagou.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ResourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    /**
     * 查询资源分类信息
     *
     * @return
     */
    @RequestMapping("/findAllResourceCategory")
    public ResponseResult findAllResourceCategory() {

        List<ResourceCategory> list = resourceCategoryService.findAllResourceCategory();
        return new ResponseResult(true, 200, "查询资源分类信息", list);
    }

    /**
     * 添加&修改资源分类
     *
     * @param resourceCategory
     * @return
     */
    @RequestMapping("/saveOrUpdateResourceCategory")
    public ResponseResult saveOrUpdateResourceCategory(@RequestBody ResourceCategory resourceCategory) {

        String msg;
        if (resourceCategory.getId() == null) {

            resourceCategoryService.saveResourceCategory(resourceCategory);
            msg = "添加资源分类";
        } else {

            resourceCategoryService.updateResourceCategory(resourceCategory);
            msg = "修改资源分类";
        }

        return new ResponseResult(true, 200, msg, null);
    }

    /**
     * 删除资源分类
     * @param id 资源分类id
     * @return
     */
    @RequestMapping("/deleteResourceCategory")
    public ResponseResult deleteResourceCategory(Integer id) {

        resourceCategoryService.deleteResourceCategory(id);
        return new ResponseResult(true, 200, "删除资源分类", null);
    }
}
